package me.leiusual.sharing.ui.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.storeindex.StoreIndex
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.StoreRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/15
 */
class HomeHomeViewModel @Inject constructor(
    private val storeRepository: StoreRepository,
    val store: Store
) : ViewModel() {

  // 首页

  private val _storeIndex = MutableLiveData<ParamsStoreIndex>()
  val storeIndex: LiveData<Data<StoreIndex>> = switchMap(_storeIndex) {
    storeRepository.storeIndex(it.bindStoreId, it.storeId).toLiveData()
  }

  fun storeIndex(bindStoreId: Long, storeId: Long) {
    _storeIndex.value = ParamsStoreIndex(bindStoreId, storeId)
  }

  data class ParamsStoreIndex(val bindStoreId: Long, val storeId: Long)
}